
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;

public class HelpScreen extends GameCanvas implements Runnable, CommandListener {

    private int delay = 100;
    private Image backGround1;
    private Image backGround2;
    private Graphics g;
    private boolean isActive;
    private Main iMain;
    private MenuScreen menuScreen;
    private Command cmdBack;
    private int chon;
    private Player clickSound;

    public HelpScreen(Main aMain, MenuScreen aMenu) {
        super(true);
        setFullScreenMode(true);
        iMain = aMain;
        menuScreen = aMenu;

        cmdBack = new Command("OK", Command.EXIT, 0);
        this.addCommand(cmdBack);
        setCommandListener(this);
    }

    public void Start() {
        try {
            backGround1 = Image.createImage("help1.png");
            backGround2 = Image.createImage("help2.png");
        } catch (Exception e) {
        };

        try {
            //moveSound = Manager.c
            InputStream in2 = getClass().getResourceAsStream("/click.wav");
            clickSound = Manager.createPlayer(in2, "audio/x-wav");
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (MediaException ex) {
            ex.printStackTrace();
        }

        chon = 1;
        isActive = true;
        Thread t = new Thread(this);
        t.start();
    }

    public void run() {
        Draw(backGround1);
        while (isActive) {
            processKeys(getKeyStates());
            flushGraphics();
            try {
                Thread.sleep(delay);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }

    public void processKeys(int keyCode) {
        switch (keyCode) {
            case LEFT_PRESSED:
                if (chon == 2) {
                    Draw(backGround1);
                    chon = 1;
                }
                if (OptionScreen.getChon() == 1) {
                    try {
                        clickSound.start();
                    } catch (MediaException ex) {
                        ex.printStackTrace();
                    }
                }
                break;
            case RIGHT_PRESSED:
                if (chon == 1) {
                    Draw(backGround2);
                    chon = 2;
                }
                if (OptionScreen.getChon() == 1) {
                    try {
                        clickSound.start();
                    } catch (MediaException ex) {
                        ex.printStackTrace();
                    }
                }
                break;
        }
    }

    public void Draw(Image img) {
        this.g = this.getGraphics();

        g.drawImage(img, 0, 0, Graphics.LEFT | Graphics.TOP);
        this.flushGraphics();
    }

    public void commandAction(Command command, Displayable displayable) {
        if (command == cmdBack) {
            if (OptionScreen.getChon() == 1) {
                try {
                    clickSound.start();
                } catch (MediaException ex) {
                    ex.printStackTrace();
                }
            }
            Display.getDisplay(iMain).setCurrent(menuScreen);
            stop();
        }
    }

    public void stop() {
        isActive = false;
    }
}
